package com.tust.controller;

import com.tust.domain.Teacher;
import com.tust.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class TeacherController {
    private String userName;
    @Autowired
    private TeacherService teacherService;

    @ResponseBody
    @RequestMapping("/selectTeacherAll.do")
    public List<Teacher> selectTeacherAll(String userName) {
        List<Teacher> teachers = teacherService.selectTeacherAll();
        this.userName = userName;
        return teachers;
    }

    @ResponseBody
    @RequestMapping("/insertTeacher.do")
    public String insertTeacher(String teacherNum, String teacherName,
                                String teacherSex, String teacherTitle,
                                String teacherEducation, String teacherBirthday,
                                String teacherDepartment) {
        String msg =null;
        List<Teacher> teacher = teacherService.selectTeacherByTeacherNum(teacherNum);
        if (teacher.size() == 1) {//教师号存在
            msg = "teacherNumExist";
        } else {
            Teacher newTeacher = new Teacher(teacherNum, teacherName, teacherSex,
                    teacherTitle, teacherEducation, teacherBirthday, teacherDepartment);
            if (teacherService.insertTeacher(newTeacher) == 1) {//插入成功
                msg = "insertSuccess";
            } else {//插入失败
                msg = "insertFalse";
            }
        }
        return msg;
    }

    @ResponseBody
    @RequestMapping("/updateTeacher.do")
    public String updateTeacher(String oldTeacherNum, String teacherNum,
                                String teacherName, String teacherSex,
                                String teacherTitle, String teacherEducation,
                                String teacherBirthday, String teacherDepartment) {
        String msg = null;
        List<Teacher> teacher = teacherService.selectTeacherByTeacherNum(teacherNum);
        if (teacher.size() == 1 && !oldTeacherNum.equals(teacherNum)) {//新教师号存在
            msg = "newTeacherNumExist";
        } else {
            Teacher newTeacher = new Teacher(teacherNum, teacherName, teacherSex,
                    teacherTitle, teacherEducation, teacherBirthday, teacherDepartment);
            if (teacherService.updateTeacher(oldTeacherNum, newTeacher) == 1) {//修改成功
                msg = "updateSuccess";
            } else {//修改失败
                msg = "updateFalse";
            }
        }
        return msg;
    }

    @ResponseBody
    @RequestMapping("/selectTeacher.do")
    public List<Teacher> selectTeacher(String teacherNum, String teacherName,
                                       String teacherSex, String teacherTitle,
                                       String teacherEducation, String teacherBirthday,
                                       String teacherDepartment) {
        List<Teacher> teacher = null;
        if (!teacherNum.equals("")) {//查询教师号
            teacher = teacherService.selectTeacherByTeacherNum(teacherNum);
        } else if (!teacherName.equals("")) {//查询教师名
            teacher = teacherService.selectTeacherByTeacherName(teacherName);
        } else if (!teacherSex.equals("")) {//查询性别
            teacher = teacherService.selectTeacherByTeacherSex(teacherSex);
        } else if (!teacherTitle.equals("")) {//查询职称
            teacher = teacherService.selectTeacherByTeacherTitle(teacherTitle);
        } else if (!teacherEducation.equals("")) {//查询学历
            teacher = teacherService.selectTeacherByTeacherEducation(teacherEducation);
        } else if (!teacherBirthday.equals("")) {//查询出生日期
            teacher = teacherService.selectTeacherByTeacherBirthday(teacherBirthday);
        } else if (!teacherDepartment.equals("")) {//查询所在部门
            teacher = teacherService.selectTeacherByTeacherDepartment(teacherDepartment);
        }
        return teacher;
    }

    @ResponseBody
    @RequestMapping("/deleteTeacher.do")
    public String deleteTeacher(String teacherNum) {
        String msg = null;
        if (teacherService.deleteTeacher(teacherNum) == 1) {//删除成功
            msg = "deleteSuccess";
        } else {//删除失败
            msg = "deleteFalse";
        }
        return msg;
    }

    @RequestMapping("/setTeacherInfoRefresh.do")
    public ModelAndView setTeacherInfoRefresh() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("setTeacherInfo");
        mv.addObject("userName", userName);
        mv.addObject("modelName", "教师信息录入");
        mv.addObject("animate","false");
        return mv;
    }
}
